<?php
/*********************/
/*                   */
/*  后台商品控制       */
/*   BY voiet        */
/*   2009-8-14更新    */
/*                   */
/*********************/

//require_once( CORE_DIR."/admin/controller/order/ctl.product.php" );
class cct_product extends ctl_product
{
	
	function singlebatchedit( $editType = "" )
				{
								if ( count( $_POST['goods_id'] ) == 0 && $_POST['_finder']['select'] != "multi" && !$_POST['_finder']['id'] && !$_POST['filter'] )
								{
												echo __( "请选择商品记录" );
												exit( );
								}
								if ( $_POST['filter'] )
								{
												$GLOBALS['_POST']['_finder'] = unserialize( stripslashes( $_POST['filter'] ) );
												$editType = $_POST['updateAct'];
								}
								if ( $_GET['cat_id'] )
								{
												$GLOBALS['_POST']['cat_id'] = $_GET['cat_id'];
								}
								$this->pagedata['filter'] = htmlspecialchars( serialize( $_POST ) );
								$this->pagedata['editInfo'] = $this->model->batcheditcols( $_POST );
								$oPro = $this->system->loadmodel( "goods/products" );
								$GLOBALS['_POST']['cat_id'] = array(
												$_GET['cat_id']
								);
								switch ( $editType )
								{
								case "uniformPrice" :
												$oLevel = $this->system->loadmodel( "member/level" );
												$priceList = array( "mktprice" => "市场价", "price" => "销售价", "cost" => "成本价" );
												$levelList = $oLevel->getmlevel( );
												foreach ( $levelList as $v )
												{
																$priceList[$v['member_lv_id']] = $v['name']."价";
												}
												$this->pagedata['updateName'] = $priceList;
												$this->pagedata['operator'] = array( "+" => "+", "-" => "-", "*" => "x" );
												break;
								case "differencePrice" :
												$count = 0;
												$page = $_POST['pagenum'] ? $_POST['pagenum'] : 1;
												if ( $_POST['pagenum'] )
												{
																$oPro->batchupdateprice( $_POST['price'] );
																$editType .= "List";
																$GLOBALS['_POST'] = $_POST['_finder'];
												}
												$goodsList = $oPro->getlist( "goods_id, name, bn, mktprice, price, cost", $_POST, ( $page - 1 ) * 20, 20, $count );
												$goodsId = array_map( create_function( "\$r", "return\$r[\"goods_id\"];" ), $goodsList );
												$productList = $oPro->getproductlvprice( $goodsId );
												$oLevel = $this->system->loadmodel( "member/level" );
												$levelList = $oLevel->getmlevel( );
												$pager = array(
																"current" => $page,
																"total" => ceil( $count / 20 ),
																"link" => "javascript:\$('pagenum').value=_PPP_;W.page('index.php?ctl=goods/product&act=singleBatchEdit', {update:\$('dialogContent'), data:\$('dialogContent'), method:'post'});",
																"token" => "_PPP_"
												);
												$this->pagedata['levelList'] = $levelList;
												$this->pagedata['goodsList'] = $goodsList;
												$this->pagedata['productList'] = $productList;
												$this->pagedata['page'] = $page;
												$this->pagedata['pager'] = $pager;
												break;
								case "uniformStore" :
												$this->pagedata['operator'] = array( "+" => "+", "-" => "-" );
												break;
								case "differenceStore" :
												$count = 0;
												$page = $_POST['pagenum'] ? $_POST['pagenum'] : 1;
												if ( $_POST['pagenum'] )
												{
																$oPro->batchupdatestore( $_POST['store'] );
																$oPro->synchronizationstore( array_keys( $_POST['store'] ) );
																$editType .= "List";
																$GLOBALS['_POST'] = $_POST['_finder'];
												}
												$goodsList = $oPro->getlist( "goods_id, name, bn", $_POST, ( $page - 1 ) * 20, 20, $count );
												$goodsId = array_map( create_function( "\$r", "return\$r[\"goods_id\"];" ), $goodsList );
												$productList = $oPro->getproductstore( $goodsId );
												$this->pagedata['goodsList'] = $goodsList;
												$this->pagedata['productList'] = $productList;
												$pager = array(
																"current" => $page,
																"total" => ceil( $count / 20 ),
																"link" => "javascript:\$('pagenum').value=_PPP_;W.page('index.php?ctl=goods/product&act=singleBatchEdit', {update:\$('dialogContent'), data:\$('dialogContent'), method:'post'});",
																"token" => "_PPP_"
												);
												$this->pagedata['page'] = $page;
												$this->pagedata['pager'] = $pager;
												break;
								case "name" :
												break;
								case "cat" :
												$oCat = $this->system->loadmodel( "goods/productCat" );
												$catMap = $oCat->getmaptree( );
												$catList = array( );
												foreach ( $catMap as $v )
												{
																$catList[$v['cat_id']] = $v['pid'] == "0" ? $v['cat_name'] : $v['type_name'].$v['cat_name'];
												}
												$this->pagedata['cat'] = $catList;
												break;
								case "brief" :
												break;
								case "unit" :
												break;
								case "dorder" :
												break;
								case "brand" :
												$oBrand = $this->system->loadmodel( "goods/brand" );
												$brandMap = $oBrand->getall( );
												$brandList = array( );
												foreach ( $brandMap as $v )
												{
																$brandList[$v['brand_id']] = $v['brand_name'];
												}
												$this->pagedata['brand'] = $brandList;
												break;
								case "score" :
												$this->pagedata['operator'] = array( "+" => "+", "-" => "-", "*" => "x" );
												break;
								case "weight" :
												$this->pagedata['operator'] = array( "+" => "+", "-" => "-", "*" => "x" );
								}
								$this->pagedata['finder'] = stripslashes( $_GET['finder'] );
								$this->setview( "product/batchEdit".ucfirst( $editType ).".html" );
								$this->output( );
				}
				
				function savebatchedit( )
				{
								$filter = unserialize( stripslashes( $_POST['filter'] ) );
								$oPro = $this->system->loadmodel( "goods/products" );
								if ( !in_array( $_POST['updateAct'], array( "differencePrice", "differenceStore" ) ) && $filter['_finder']['select'] == "multi" )
								{
												$filter['goods_id'] = $oPro->getgoodsidbyfilter( $filter );
								}
								$haserror = false;
								switch ( $_POST['updateAct'] )
								{
								case "uniformPrice" :
												if ( is_numeric( $_POST['updateName'][$_POST['updateType']] ) )
												{
																$oPro->batchupdatememberpricebyoperator( $filter['goods_id'], $_POST['updateName'][$_POST['updateType']], abs( floatval( $_POST['set'][$_POST['updateType']] ) ), $_POST['operator'][$_POST['updateType']], $_POST['fromName'][$_POST['updateType']] );
												}
												else
												{
																$oPro->batchupdatebyoperator( $filter['goods_id'], "sdb_goods", $_POST['updateName'][$_POST['updateType']], abs( floatval( $_POST['set'][$_POST['updateType']] ) ), $_POST['operator'][$_POST['updateType']], $_POST['fromName'][$_POST['updateType']] );
																if ( !( $_POST['updateName'][$_POST['updateType']] != "mktprice" ) )
																{
																				break;
																}
																$tableName = "sdb_products";
																if ( $_POST['fromName'][$_POST['updateType']] == "mktprice" )
																{
																				$tableName = "sdb_goods a, sdb_products b";
																				$GLOBALS['_POST']['updateName'][$_POST['updateType']] = "b.".$_POST['updateName'][$_POST['updateType']];
																}
																$oPro->batchupdatebyoperator( $filter['goods_id'], $tableName, $_POST['updateName'][$_POST['updateType']], abs( floatval( $_POST['set'][$_POST['updateType']] ) ), $_POST['operator'][$_POST['updateType']], $_POST['fromName'][$_POST['updateType']] );
												}
												break;
								case "differencePrice" :
												$oPro->batchupdateprice( $_POST['price'] );
												break;
								case "uniformStore" :
												$oPro->batchupdatebyoperator( $filter['goods_id'], "sdb_products", "store", abs( intval( $_POST['set'][$_POST['updateType']] ) ), $_POST['operator'][$_POST['updateType']], $_POST['fromName'][$_POST['updateType']] );
												$oPro->synchronizationstore( $filter['goods_id'] );
												break;
								case "differenceStore" :
												$oPro->batchupdatestore( $_POST['store'] );
												$oPro->synchronizationstore( array_keys( $_POST['store'] ) );
												break;
								case "name" :
												if ( !( $_POST['updateType'] != "name" || $_POST['set']['name'] != "" ) )
												{
																break;
												}
												$oPro->batchupdatetext( $filter['goods_id'], $_POST['updateType'], "name", $_POST['set'][$_POST['updateType']] );
												break;
								case "cat" :
												$oPro->batchupdateint( $filter['goods_id'], "cat_id", intval( $_POST['set']['cat'] ) );
												break;
								case "brief" :
												$oPro->batchupdatetext( $filter['goods_id'], $_POST['updateType'], "brief", $_POST['set'][$_POST['updateType']] );
												break;
								case "unit" :
												$oPro->batchupdatetext( $filter['goods_id'], $_POST['updateType'], "unit", $_POST['set'][$_POST['updateType']] );
												break;
								case "brand" :
												$oBrand = $this->system->loadmodel( "goods/brand" );
												$aBrand = $oBrand->getfieldbyid( intval( $_POST['set']['brand'] ), array( "brand_name" ) );
												$oPro->batchupdatearray( $filter['goods_id'], "sdb_goods", array(
																"brand_id",
																"brand"
												), array(
																intval( $_POST['set']['brand'] ),
																$aBrand['brand_name']
												) );
												break;
								case "dorder" :
												$oPro->batchupdateint( $filter['goods_id'], "d_order", intval( $_POST['set']['dorder'] ) );
												break;
								case "score" :
												$oPro->batchupdatebyoperator( $filter['goods_id'], "sdb_goods", "score", abs( intval( $_POST['set'][$_POST['updateType']] ) ), $_POST['operator'][$_POST['updateType']] );
												break;
								case "weight" :
												$oPro->batchupdatebyoperator( $filter['goods_id'], "sdb_goods", "weight", abs( floatval( $_POST['set'][$_POST['updateType']] ) ), $_POST['operator'][$_POST['updateType']] );
												$oPro->batchupdatebyoperator( $filter['goods_id'], "sdb_products", "weight", abs( floatval( $_POST['set'][$_POST['updateType']] ) ), $_POST['operator'][$_POST['updateType']] );
								}
								ini_set( "track_errors", "1" );
								restore_error_handler( );
								if ( !$haserror )
								{
												echo "ok";
								}
								else
								{
												echo $php_errormsg;
								}
				}
}

?>